package com.jie.jie.yunxin;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * author:jmm
 * date:2016/12/20 16:47
 * e-mail:jianmingmi@foxmail.com
 */

//        @GET("book/search")
//        Call<String> getString(@Query("q") String name);
//        @GET("book/search")
//        Observable<String> getAAA(@Query("q") String name);
//
// 使用方法1：异步
//        XiaohuaRetrofitManager
//                .getInstance()
//                .createService(Test.BlueService.class)
//                .getAAA("")
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//
//                    }
//                });
//
// 使用方法1：同步
//            Response<String> response = XiaohuaRetrofitManager
//                    .getInstance()
//                    .createService(BlueService.class)
//                    .getString("")
//                    .execute();

/**
 * https://api.netease.im/nimserver/user/create.action 创建云信ID accid praps icon token
 * https://api.netease.im/nimserver/user/update.action 云信ID更新 accid
 * https://api.netease.im/nimserver/user/refreshToken.action  更新accid
 * https://api.netease.im/nimserver/msg/sendMsg.action 发送普通消息
 */

public class YXRetrofitManager {
    private static YXRetrofitManager mRetrofitManager;
    private Retrofit mRetrofit;
    private static  String BASE_URL = "https://api.netease.im/nimserver/";
//    private static  String BASE_URL = "http://www.mmjpg.com/home/";
//    private static String BASE_URL = "http://www.mmjpg.com/mm/873/";


    private YXRetrofitManager() {
        initRetrofit();
    }

    public static synchronized YXRetrofitManager getInstance() {
            mRetrofitManager = new YXRetrofitManager();
        return mRetrofitManager;
    }


    private void initRetrofit() {
        // 消息拦截器
        HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
//        LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        // 连接操作
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(9, TimeUnit.SECONDS)
                .writeTimeout(9, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                // 增加Bean支持返回
//                .addConverterFactory(GsonConverterFactory.create())
                // 增加String返回支持
                .addConverterFactory(ScalarsConverterFactory.create())
                // RxJava适配
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
    }

    public <T> T createService(Class<T> reqServer) {
        return mRetrofit.create(reqServer);
    }
}